﻿using System;
using Devonline.Core;

namespace Devonline.Identity
{
    public static class IdentityExtensions
    {
        /// <summary>
        /// 针对字符串类型主键的 IIdentity 实例获取对应的 AssignType
        /// </summary>
        /// <param name="identity"></param>
        /// <returns></returns>
        public static AssignType GetAssignType(this IIdentity<string> identity) => identity.GetAssignType<string>();

        /// <summary>
        /// 针对 IIdentity<TKey> 实例获取对应的 AssignType
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="identity"></param>
        /// <returns></returns>
        public static AssignType GetAssignType<TKey>(this IIdentity<TKey> identity) where TKey : IConvertible, IEquatable<TKey> => identity.GetAssignType<IIdentity<TKey>, TKey>();

        /// <summary>
        /// 针对任意引用类型获取对应的身份 AssignType
        /// </summary>
        /// <typeparam name="TIdentity"></typeparam>
        /// <param name="identity"></param>
        /// <returns></returns>
        public static AssignType GetAssignType<TIdentity, TKey>(this TIdentity identity) where TIdentity : class, IIdentity<TKey> where TKey : IConvertible, IEquatable<TKey> => identity switch
        {
            User<TKey> => AssignType.User,
            Role<TKey> => AssignType.Role,
            Group<TKey> => AssignType.Group,
            Level<TKey> => AssignType.Level,
            _ => AssignType.System
        };
    }
}
